package com.bbzn.device.client.service.emq.order.impl.mq;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.DeviceConstant;
import com.bbzn.device.client.dataobject.Device;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.DeviceService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.OptionalBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Author wulongbo
 * @Date 2021/12/16 16:19
 * @Version 1.0
 */
@Component
public class MqSetEnvironTempImpl extends BaseMqSendImpl {

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private DeviceService deviceService;

    @Override
    public String checkMap(String language) throws Exception {
        if (!OptionalBean.ofNullable(map.get("temperatureUpper")).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_UPPER_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(map.get("temperatureLower")).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_LOWER_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        BigDecimal temperatureUpper = new BigDecimal(map.get("temperatureUpper").toString());
        BigDecimal temperatureLower = new BigDecimal(map.get("temperatureLower").toString());
        Device device = new Device();
        device.setImei(map.get("imei").toString());
        device.setTemperatureUpper(temperatureUpper.doubleValue());
        device.setTemperatureLower(temperatureLower.doubleValue());
        // 室外温度间隔设置状态【0：修改成功 / 1：修改中】
        device.setTemperatureState(1);
        if (OptionalBean.ofNullable(map.get("interval")).isPresent()) {
            device.setTemperatureInterval(Integer.parseInt(map.get("interval").toString()));
        }

        // 1.修改室外温度值
        deviceService.updateByImeiSelective(device);
        return JSONObject.toJSONString(map);
    }
}
